//
//  XGTabBarController.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/10.
//  Copyright © 2018 itcast. All rights reserved.
//

#import "XGTabBarController.h"
#import "XGNavigationController.h"

#import "XGEssenceViewController.h"

#import "XGTabBar.h"

@interface XGTabBarController ()


@end

@implementation XGTabBarController

#pragma mark - 控制器生命周期方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置tabBar
    [self setUpTabBar];
    // 添加子控制器
    [self addAllChildViewControllers];
    // 注册通知监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tapAdvertisementNotification:) name:kTapAdvertisementNotificationName object:nil];
}

- (void)dealloc
{
    // 注销通知监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 通知监听
- (void)tapAdvertisementNotification:(NSNotification *)notification
{
    NSURL *webUrl = notification.userInfo[kAdvertisementWebUrlKey];
    XGNavigationController *nav = self.childViewControllers.firstObject;
    [(XGEssenceViewController *)nav.topViewController setAdvertisementWebUrl:webUrl];
}

#pragma mark - 设置TabBarController

- (void)setUpTabBar
{
    // 删除系统自带的tabBar 使用我们自定义的tabBar
    XGTabBar *tabBar = [[XGTabBar alloc] init];
    [self.tabBar removeFromSuperview];
    [self setValue:tabBar forKey:@"tabBar"];
}

/**
 *  添加所有子控制器
 */
- (void)addAllChildViewControllers
{
    [self addChildViewControllerClassName:@"XGEssenceViewController" title:@"精华" imageName:@"tabBar_essence_icon" selectedImageName:@"tabBar_essence_click_icon"];
    [self addChildViewControllerClassName:@"XGNewViewController" title:@"新帖" imageName:@"tabBar_new_icon" selectedImageName:@"tabBar_new_click_icon"];
    [self addChildViewControllerClassName:@"XGFocusViewController" title:@"关注" imageName:@"tabBar_friendTrends_icon" selectedImageName:@"tabBar_friendTrends_click_icon"];
    [self addChildViewControllerClassName:@"XGMineTableViewController" title:@"我的" imageName:@"tabBar_me_icon" selectedImageName:@"tabBar_me_click_icon"];
}


/**
 添加子控件

 @param className 子控制器类名
 @param title 标题
 @param imageName 图片名称
 @param selectedImageName 选中图片名称
 */
- (void)addChildViewControllerClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    UIViewController *viewController = nil;
    if ([className isEqualToString:@"XGMineTableViewController"]) {
        viewController = [[UIStoryboard storyboardWithName:className bundle:nil] instantiateInitialViewController];
    } else {
        viewController = [[NSClassFromString(className) alloc] init];
    }
    
    viewController.navigationItem.title = title;
    viewController.tabBarItem.title = title;
    viewController.tabBarItem.image = [UIImage imageNamed:imageName];
    viewController.tabBarItem.selectedImage = [UIImage originalImageWithImageName:selectedImageName];
    XGNavigationController *nav = [[XGNavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:nav];
}

#pragma mark - 其他方法

+ (void)initialize
{
    // 设置tabbar按钮的文字属性
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName : [UIColor lightGrayColor],
                                 NSFontAttributeName : [UIFont systemFontOfSize:15]};
    [tabBarItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
}

@end
